<?php
/**
 *
 * Auth: feikor
 * Date: 2023/06/12
 */

namespace app\model;

use think\Model;

class Actionlog extends Model
{
	// 设置当前模型对应的数据表
	protected $name = 'actionlog';
	// 设置当前模型主键
	protected $pk = 'id';

	/**
	 * 根据id获取数据
	 *
	 * @param int $id
	 * @param string $fields
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function getInfoById(int $id, string $fields = 'id')
	{
		$data = $this::where(['id' => $id])->field($fields)->find();
		return $data ? $data->toArray() : [];
	}

	/**
	 * 新增/更新
	 *
	 * @dateTime 2022/08/24
	 * @param array $params
	 * @return fixed
	 */
	public function edit($params)
	{
		if (empty($params['id'])) {
			$data = $this::create($params);
			return $data->id;
		}
		$this::update($params);
		return $params['id'];
	}

	// 获取分页数据
	public function getPagingDataByWhere($where = [], $page = 1, $limit = 10, string $fields = '*', $order = 'id desc')
	{
		$total = $this::where($where)->count();
		if ($total == 0) {
			return ['total'      => 0,
					'total_page' => 0,
					'data'       => []
			];
		}
		$offset = ($page - 1) * $limit;
		$totalPage = ceil($total / $limit);
		$data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();
		return ['total'      => $total,
				'total_page' => $totalPage,
				'data'       => $data
		];
	}

	// 获取所有数据
	public function getAllData($where, $fields = 'id', $order = 'id desc')
	{
		$data = $this::where($where)->field($fields)->order($order)->select();

		return $data ? $data->toArray() : [];
	}


}